package com.lanhe.apply.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lanhe.apply.domain.LhQingchuangApply;
import com.lanhe.apply.domain.LhQingchuangApplyLog;
import com.lanhe.apply.domain.LhQingchuangApplyPool;
import com.lanhe.apply.domain.dto.LhOrdersQingChuangApplyDto;
import com.lanhe.apply.domain.vo.LhQingchuangApplyTextVo;
import com.lanhe.apply.mapper.LhQingchuangApplyMapper;
import com.lanhe.apply.service.ILhQingchuangApplyLogService;
import com.lanhe.apply.service.ILhQingchuangApplyPoolService;
import com.lanhe.apply.service.ILhQingchuangApplyService;
import com.lanhe.common.constant.Constants;
import com.lanhe.common.constant.HttpStatus;
import com.lanhe.common.exception.ServiceException;
import com.lanhe.common.utils.StringUtils;
import com.lanhe.common.utils.qingchuang.QingChuangUtil;
import com.lanhe.common.utils.qingchuang.dto.QingChuangApplyDTO;
import com.lanhe.common.utils.qingchuang.vo.QingChuangApplyVO;
import com.lanhe.product.domain.LhQingchuangProductDetails;
import com.lanhe.product.service.ILhQingchuangProductDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * (LhQingchuangApply)表服务实现类
 *
 * @author zhanggl
 * @since 2023-12-15 19:25:08
 */
@Service
public class LhQingchuangApplyServiceImpl extends ServiceImpl<LhQingchuangApplyMapper, LhQingchuangApply> implements ILhQingchuangApplyService {

	@Autowired
	private LhQingchuangApplyMapper applyMapper;

	@Autowired
	private QingChuangUtil qingChuangUtil;

	@Autowired
	private ILhQingchuangApplyLogService applyCallbackLogService;

	@Autowired
	private ILhQingchuangApplyPoolService applyPoolService;

	@Autowired
	private ILhQingchuangProductDetailsService productDetailsService;

	/**
	 * 新增报备数据
	 *
	 * @param applyDto DTO
	 * @return 结果
	 * @author zhanggl
	 * @date 2023/12/15
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public String insertApply(QingChuangApplyDTO applyDto) throws Exception {

		String salesman = "";
		for (QingChuangApplyDTO.TextBean textBean : applyDto.getText()) {
			if ("业务员姓名".equals(textBean.getLabel())) {
				salesman = textBean.getValue();
			}
		}
		LambdaQueryWrapper<LhQingchuangApply> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(LhQingchuangApply::getNumber,applyDto.getNumber());
		queryWrapper.eq(LhQingchuangApply::getMemberId,applyDto.getMemberId());
		List<LhQingchuangApply> lhQingchuangApplies = applyMapper.selectList(queryWrapper);
		for (LhQingchuangApply lhQingchuangApply : lhQingchuangApplies) {
			String text = lhQingchuangApply.getText();
			List<LhQingchuangApplyTextVo> lhQingchuangApplyTextVos = JSONArray.parseArray(text, LhQingchuangApplyTextVo.class);
			for (LhQingchuangApplyTextVo lhQingchuangApplyTextVo : lhQingchuangApplyTextVos) {
				if ("业务员姓名".equals(lhQingchuangApplyTextVo.getLabel())) {
					if (lhQingchuangApplyTextVo.getValue().equals(salesman)){
						throw new ServiceException("业务员=>"+salesman+"已报备,请勿重复报备", HttpStatus.ERROR);
					}
				}
			}
		}

		String resource = qingChuangUtil.apply(applyDto);
		QingChuangApplyVO applyVO = JSONObject.parseObject(resource, QingChuangApplyVO.class);
		if (!Constants.GeneralIntType.ONE.equals(applyVO.getCode())) {
			throw new ServiceException("报备异常->" + applyVO.getMsg(), HttpStatus.ERROR);
		}

		LhQingchuangProductDetails productDetails = productDetailsService.selectProductDetailByNumber(applyDto.getNumber());
		if (ObjectUtil.isEmpty(productDetails)) {
			throw new ServiceException("详情数据查询异常", HttpStatus.ERROR);
		}

		LhQingchuangApplyPool applyPool = new LhQingchuangApplyPool();
		applyPool.setNumber(applyDto.getNumber());
		applyPool.setPid(productDetails.getId());
		applyPool.setTitle(productDetails.getTitle());
		applyPool.setSubtitle(productDetails.getSubtitle());
		applyPool.setThmub(productDetails.getThmub());
		applyPool.setMemberId(applyDto.getMemberId());
		LhQingchuangApplyPool lhQingchuangApplyPool = applyPoolService.saveApply(applyPool);
		if (ObjectUtil.isNull(lhQingchuangApplyPool)){
			throw new ServiceException("主表数据异常", HttpStatus.ERROR);
		}

		LhQingchuangApply qingchuangApply = new LhQingchuangApply();
		qingchuangApply.setApplyId(applyVO.getData());
		qingchuangApply.setQingchuangApplyPoolId(lhQingchuangApplyPool.getQingchuangApplyPoolId());
		qingchuangApply.setNumber(applyDto.getNumber());
		qingchuangApply.setTitle(productDetails.getTitle());
		qingchuangApply.setMemberId(applyDto.getMemberId());
		qingchuangApply.setType(applyDto.getType());
		qingchuangApply.setMobile(applyDto.getMobile());
		qingchuangApply.setSalesman(salesman);
		qingchuangApply.setText(JSON.toJSONString(applyDto.getText()));
		qingchuangApply.setSubTitle(productDetails.getSubTemplateTitle());
		qingchuangApply.setSubTemplate(productDetails.getSubTemplate());
		qingchuangApply.setApplyExpectTime(productDetails.getApplyExpectTime());
		qingchuangApply.setStatus(Constants.APPLY_STATUS_0);
		qingchuangApply.setCreateTime(new Date());
		applyMapper.insert(qingchuangApply);
		return applyVO.getData();
	}

	/**
	 * 报备数据回调
	 *
	 * @param qingChuangApplyDto DTO
	 * @author zhanggl
	 * @date 2023/12/15
	 */
	@Override
	public void applyNotice(LhOrdersQingChuangApplyDto qingChuangApplyDto) {

		LhQingchuangApplyLog applyCallbackLog = new LhQingchuangApplyLog();
		applyCallbackLog.setApikey(qingChuangApplyDto.getApikey());
		applyCallbackLog.setId(qingChuangApplyDto.getId());
		applyCallbackLog.setLink(qingChuangApplyDto.getLink());
		applyCallbackLog.setStatus(qingChuangApplyDto.getStatus());
		applyCallbackLog.setReason(qingChuangApplyDto.getReason());
		LhQingchuangApplyLog applyCallbackLogRet = applyCallbackLogService.insertApplyCallbackLog(applyCallbackLog);

		if (ObjectUtil.isNotNull(applyCallbackLogRet) && qingChuangUtil.getAppSecret().equals(qingChuangApplyDto.getApikey())) {
			LambdaQueryWrapper<LhQingchuangApply> lambdaQueryWrapper = new LambdaQueryWrapper<>();
			lambdaQueryWrapper.eq(LhQingchuangApply::getApplyId, qingChuangApplyDto.getId());
			LhQingchuangApply qingchuangApply = applyMapper.selectOne(lambdaQueryWrapper);
			if (ObjectUtil.isNotNull(qingchuangApply)) {
				qingchuangApply.setApplyId(qingChuangApplyDto.getId());
				qingchuangApply.setLink(qingChuangApplyDto.getLink());
				qingchuangApply.setStatus(qingChuangApplyDto.getStatus());
				qingchuangApply.setReason(qingChuangApplyDto.getReason());
				qingchuangApply.setUpdateTime(new Date());
				applyMapper.update(qingchuangApply, lambdaQueryWrapper);
			}
		}
	}

	/**
	 * 报备数据列表
	 *
	 * @param userKey  用户KEY
	 * @param pid      产品ID
	 * @param status   状态
	 * @param pageNum  页面码
	 * @param pageSize 条数
	 * @return 结果
	 * @author zhanggl
	 * @date 2023/12/18
	 */
	@Override
	public String applyList(String userKey, Long pid, Long status, Integer pageNum, Integer pageSize) {

		return qingChuangUtil.applyList(userKey, pid, status, pageNum, pageSize);
	}

	/**
	 * 报备数据列表
	 *
	 * @param apply 实体
	 * @return java.util.List<com.lanhe.article.domain.LhArticle>
	 * @author zhanggl
	 * @date 2023/12/19
	 */
	@Override
	public List<LhQingchuangApply> selectApplyList(LhQingchuangApply apply) {
		LambdaQueryWrapper<LhQingchuangApply> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(StringUtils.isNotBlank(apply.getNumber()),LhQingchuangApply::getNumber, apply.getNumber());
		queryWrapper.eq(ObjectUtil.isNotNull(apply.getMemberId()),LhQingchuangApply::getMemberId, apply.getMemberId());
		queryWrapper.orderByDesc(LhQingchuangApply::getQingchuangApplyId);
		return applyMapper.selectList(queryWrapper);
	}

	/**
	 * 根据手机号和业务员查询数据
	 *
	 * @param mobile   手机号
	 * @param salesman 业务员
	 * @return java.util.List<com.lanhe.article.domain.LhArticle>
	 * @author zhanggl
	 * @date 2023年12月21日23:29:14
	 */
	@Override
	public LhQingchuangApply selectApplyByMobileAndSalesman(String mobile, String salesman) {
		LambdaQueryWrapper<LhQingchuangApply> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(LhQingchuangApply::getMobile, mobile);
		queryWrapper.eq(LhQingchuangApply::getSalesman, salesman);
		queryWrapper.orderByDesc(LhQingchuangApply::getQingchuangApplyId);
		queryWrapper.last("limit 1");
		return applyMapper.selectOne(queryWrapper);
	}
}

